﻿package components
{
	import com.bit101.components.Label;
	import com.bit101.components.Panel;
	import com.bit101.components.PushButton;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * ...
	 * @author s
	 */
	public class ZipPanel extends Panel
	{
		public static const STATE_ZIPPED:int = 1;
		public static const STATE_EXPANDED:int = 2;
		public static const STATE_ZIPPING:int = 3;
		public static const STATE_EXPANDING:int = 4;

		private var _titleBar:Sprite;
		private var _titleLabel:Label;
		private var _zippedRect:Rectangle;
		private var _expandedRect:Rectangle;
		private var _state:int;
		private var _motionCt:int;
		private var _motionCtMax:int;
		
		public function ZipPanel(label:String, zippedRect:Rectangle, expandedRect:Rectangle) 
		{
			super();			

			_zippedRect = zippedRect;
			_expandedRect = expandedRect;
			_state = STATE_ZIPPED;
			_motionCt = 0;
			this.x = _zippedRect.left;
			this.y = _zippedRect.top;
			this.width = _zippedRect.width;
			this.height = _zippedRect.height;
			
			//_labelButton = new PushButton(this, 0, 0, label);
			_titleLabel = new Label;
			content.addChild(_titleLabel);
			_titleLabel.autoSize = true;
			_titleLabel.text = label;
			_titleLabel.alpha = 0.7;
			_titleLabel.draw(); // drawを呼ばないとwidthが更新されない
			_titleLabel.x = (this.width - _titleLabel.width) / 2;
			_titleLabel.y = 2;
			//_labelButton.buttonMode = true;
			//_labelButton.mouseEnabled = true;
			//_labelButton.addEventListener(MouseEvent.CLICK, onClick);
			
			_titleBar = new Sprite;
			content.addChild(_titleBar);
			_titleBar.x = 0;
			_titleBar.y = 0;
			_titleBar.graphics.beginFill(0x101010, 0);
			_titleBar.graphics.drawRect(0, 0, this.width, this.height);
			_titleBar.graphics.endFill();
			_titleBar.buttonMode = true;
			_titleBar.useHandCursor = true;
			_titleBar.addEventListener(MouseEvent.CLICK, onClick);
			//this.buttonMode = true;
			//this.useHandCursor = true;
			//this.addEventListener(MouseEvent.CLICK, onClick);
		}
		
		public function zip():void 
		{
			if(_state == STATE_EXPANDED){
				_motionCt = 0;
				_motionCtMax = 10;
				_state = STATE_ZIPPING;
				super.addEventListener(Event.ENTER_FRAME, onEnterFrame);
				//setChildrenVisibility(false);
			}
		}
		
		public function expand():void 
		{
			if(_state == STATE_ZIPPED){
				_motionCt = 0;
				_motionCtMax = 10;
				_state = STATE_EXPANDING;
				super.addEventListener(Event.ENTER_FRAME, onEnterFrame);
				//setChildrenVisibility(false);
			}
		}
		
		private function setChildrenVisibility(visible:Boolean):void 
		{
			for (var i:int = 0; i < this.numChildren; i++) {
				this.getChildAt(i).visible = visible;
			}
		}
		
		private function onEnterFrame(e:Event):void 
		{
			if(_motionCt <= _motionCtMax){
				var d:Number = (1 - Math.cos(Math.PI * _motionCt / _motionCtMax)) / 2.0;
				var d2:Number = d * d * d;
				if (_state == STATE_EXPANDING) {
					super.width = int((_expandedRect.width - _zippedRect.width) * d + _zippedRect.width);
					super.height = int((_expandedRect.height - _zippedRect.height) * d2 + _zippedRect.height);
					super.x = int((_expandedRect.x - _zippedRect.x) * d + _zippedRect.x);
					super.y = int((_expandedRect.y - _zippedRect.y) * d2 + _zippedRect.y);
				}
				else if (_state == STATE_ZIPPING) {
					super.width = (_zippedRect.width - _expandedRect.width) * d2 + _expandedRect.width;
					super.height = (_zippedRect.height - _expandedRect.height) * d + _expandedRect.height;
					super.x = (_zippedRect.left - _expandedRect.x) * d2 + _expandedRect.x;
					super.y = (_zippedRect.y - _expandedRect.y) * d + _expandedRect.y;
				}
				_titleLabel.x = (this.width - _titleLabel.width) / 2;
				_titleBar.width = this.width;
				_motionCt++;
				super.draw();
			}
			else {
				if (_state == STATE_EXPANDING) {
					_state = STATE_EXPANDED;
				}
				else if (_state == STATE_ZIPPING) {
					_state = STATE_ZIPPED;
				}
				super.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
		}
		
		private function onClick(e:Event):void 
		{
			if (_state == STATE_EXPANDED) {
				this.zip();
			}
			else if (_state == STATE_ZIPPED) {
				this.expand();
			}
		}
	}

}